﻿// Decompiled with JetBrains decompiler
// Type: Netick.Unity.AdditiveSceneOperation
// Assembly: Netick.Unity, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: E128C7FD-39ED-4452-86AC-6EC748457030
// Assembly location: D:\WorkSpaceDemo\UPM\NetickForUnity\Netick\Runtime\Netick.Unity.dll
// XML documentation location: D:\WorkSpaceDemo\UPM\NetickForUnity\Netick\Runtime\Netick.Unity.xml

using UnityEngine.SceneManagement;

#nullable disable
namespace Netick.Unity;

internal class AdditiveSceneOperation : SceneOperation
{
  public NetworkSceneOperation NetworkSceneOperation;
  public int SlotIndex;
  public int SceneIndex;
  public int LoadCounterId;
  public Scene SceneToUnload;
  public LocalPhysicsMode LocalPhysicsMode;
  public Scene NewScene;
  public bool Load;
  public ISceneOperation Operation;

  public NetworkSandbox Sandbox { get; set; }

  public bool IsFinished { get; set; }

  void SceneOperation.Start()
  {
    if (!this.Load && !this.SceneToUnload.IsValid())
      return;
    if (this.Load)
      StaticUnitySceneManager.ValidateSceneId(this.Sandbox, this.SceneIndex);
    LoadSceneParameters loadSceneParameters = new LoadSceneParameters()
    {
      loadSceneMode = LoadSceneMode.Additive,
      localPhysicsMode = this.LocalPhysicsMode
    };
    if (StaticUnitySceneManager.IsCustomScene(this.SceneIndex))
    {
      int index = this.SceneIndex - SceneManager.sceneCountInBuildSettings;
      string sceneName = "";
      this.Operation = !this.Load ? this.Sandbox.NetworkSceneHandler.InvokeUnloadCustomSceneAsync(this.SceneToUnload) : this.Sandbox.NetworkSceneHandler.InvokeLoadCustomSceneAsync(index, loadSceneParameters, out sceneName);
      this.NetworkSceneOperation = new NetworkSceneOperation(this.Operation, this.Load ? SceneManager.GetSceneAt(SceneManager.sceneCount - 1) : this.SceneToUnload, this.Load, -1, sceneName, true);
    }
    else
    {
      this.Operation = !this.Load ? this.Sandbox.NetworkSceneHandler.InvokeUnloadBuildSceneAsync(this.SceneToUnload) : this.Sandbox.NetworkSceneHandler.InvokeLoadBuildSceneAsync(this.SceneIndex, loadSceneParameters);
      int num = this.Load ? this.SceneIndex : this.SceneToUnload.buildIndex;
      this.NetworkSceneOperation = new NetworkSceneOperation(this.Operation, this.Load ? SceneManager.GetSceneAt(SceneManager.sceneCount - 1) : this.SceneToUnload, this.Load, num, Network.NameFromIndex(num), true);
    }
    this.Sandbox.Events.InvokeOnSceneOperationBegan(this.Sandbox, this.NetworkSceneOperation);
  }

  void SceneOperation.Update()
  {
    if (!this.Operation.IsDone)
      return;
    if (this.Load)
      this.NewScene = SceneManager.GetSceneAt(SceneManager.sceneCount - 1);
    StaticUnitySceneManager.PendingFinishedOp = (SceneOperation) this;
    this.IsFinished = true;
  }
}
